package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.font.FontLibrary;
import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.core.toolkit.StringToolkit;
import info.thereisonlywe.quran.text.QuranicTextList;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.LayeredHighlighter;

public class SelectiveCopyDialog {

	private static JDialog dialog;
	private static JButton okButton;
	private static JScrollPane scrollPane;
	private static JTextArea textArea;
	private static JLabel lblNewLabel;
	private static LayeredHighlighter.LayerPainter highlightPainter = new MyHighlightPainter();
	private static JToggleButton chckbxNewCheckBox;
	private static String text = "";

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setResizable(false);
		dialog.setIconImage(null);
		dialog.setTitle(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "SelectiveCopy"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setPreferredSize(new Dimension(500, 300));
		dialog.setSize(new Dimension(500, 300));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 0));
		dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);

		lblNewLabel = new JLabel(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "MouseSelection"));
		chckbxNewCheckBox = new JToggleButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "RemoveAccentsAndDiacritics"));
		dialog.getContentPane().add(chckbxNewCheckBox, BorderLayout.NORTH);
		buttonPane.add(lblNewLabel);
		okButton = new JButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "OK"));
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);

		scrollPane = new JScrollPane();
		dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);

		textArea = new JTextArea();
		textArea.setCaret(new MyCaret());
		textArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		textArea.setDoubleBuffered(false);
		textArea.setOpaque(false);
		textArea.setSelectedTextColor(Color.black);
		textArea.setSelectionColor(Color.yellow);
		scrollPane.setViewportView(textArea);
		initListeners();
	}

	private static void initListeners()
	{

		chckbxNewCheckBox.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (chckbxNewCheckBox.isSelected()) textArea
						.setText(StringToolkit.removeDiacritics(text));
				else textArea.setText(text);
			}
		});

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
							{
								dialog.setVisible(false);
								UIDelegate.setOpenDialog(false);
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

		okButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				GUIToolkit.setClipboardContents(textArea.getSelectedText());
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

	}

	public static void showDialog(int callingRow)
	{
		init();
		textArea.setFont(callingRow == 0 ? FontLibrary.get(
				FontLibrary.SCHERAZADE, 28) : FontLibrary.get(
				FontLibrary.CODE2000, 18));
		scrollPane.applyComponentOrientation(callingRow == 0
				|| QuranicTextList.getText(
						QuranicTextList.getIndexByOrder(callingRow - 1))
						.isRTL() ? ComponentOrientation.RIGHT_TO_LEFT
				: ComponentOrientation.LEFT_TO_RIGHT);
		text = (String) MainFrame.view_Browser.getVersePane().getTable()
				.getValueAt(callingRow, 0);
		if (chckbxNewCheckBox.isSelected()) textArea.setText(StringToolkit
				.removeDiacritics(text));
		else textArea.setText(text);
		textArea.setCaretPosition(0);
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		UIDelegate.setOpenDialog(true);
		dialog.setVisible(true);
	}

	// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5101869
	static class MyCaret extends DefaultCaret {

		@Override
		protected Highlighter.HighlightPainter getSelectionPainter()
		{
			return highlightPainter;
		}

	}

	static class MyHighlightPainter extends
			DefaultHighlighter.DefaultHighlightPainter {
		MyHighlightPainter()
		{
			super(Color.yellow);
		}
	}

}
